package com.sto.stoapp.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.activity.sales.SalesAddSignNameActivity;
import com.sto.stoapp.db.manager.SalesSignNameManager;
import com.sto.stoapp.entity.SelectSignNameEntity;
import com.sto.stoapp.util.Helper;

public class SlectSignNameAdapter extends BaseAdapter {

	private Context context;

	private List<SelectSignNameEntity> signLists;

	private int REQUEST_CODE;

	/**
	 * 构造函数
	 * 
	 * @param context
	 * @param signLists
	 */
	public SlectSignNameAdapter(Context context,
			List<SelectSignNameEntity> signLists, int REQUEST_CODE) {
		this.context = context;
		this.signLists = signLists;
		this.REQUEST_CODE = REQUEST_CODE;
	}

	@Override
	public int getCount() {
		return signLists == null ? 0 : signLists.size();
	}

	@Override
	public Object getItem(int arg0) {
		return signLists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		Holder holder;
		if (arg1 == null) {
			holder = new Holder();
			arg1 = LayoutInflater.from(context).inflate(
					R.layout.sto_sign_name_adapter, null);
			holder.code = (TextView) arg1.findViewById(R.id.code_text);
			holder.name = (TextView) arg1.findViewById(R.id.name_text);
			holder.delete = (Button) arg1.findViewById(R.id.item_delete);
			holder.edit = (Button) arg1.findViewById(R.id.item_edit);
			holder.headLayout = (LinearLayout) arg1
					.findViewById(R.id.head_layout);
			arg1.setTag(holder);
		} else {
			holder = (Holder) arg1.getTag();
		}
		holder.code.setText(signLists.get(arg0).getCode());
		holder.name.setText(signLists.get(arg0).getName());
		if (!signLists.get(arg0).isUser()) {
			holder.delete.setVisibility(View.INVISIBLE);
			holder.edit.setVisibility(View.INVISIBLE);
		} else {
			holder.delete.setVisibility(View.VISIBLE);
			holder.edit.setVisibility(View.VISIBLE);
		}
		holder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (SalesSignNameManager.getInstance(context).delete(
						signLists.get(arg0))) {
					signLists.remove(arg0);
					notifyDataSetChanged();
					Helper.showToast(context,
							context.getString(R.string.delete_success));
				} else {
					Helper.showToast(context,
							context.getString(R.string.delete_failure));
				}
			}
		});

		holder.edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,
						SalesAddSignNameActivity.class);
				intent.putExtra(SalesAddSignNameActivity.SIGN_NAME,
						signLists.get(arg0));
				((Activity) context).startActivityForResult(intent,
						REQUEST_CODE);
			}
		});
		holder.headLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Toast.makeText(context, "hello", Toast.LENGTH_LONG).show();

			}
		});
		return arg1;
	}

	class Holder {
		TextView code;
		TextView name;
		Button delete;
		Button edit;
		LinearLayout headLayout;
	}
}
